<template>
  <div class="mod-config">
    <el-form :inline="true" :model="listQuery" @keyup.enter.native="handleFilter()">
      <el-form-item>
        <el-input v-model="listQuery.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleFilter()">{{text_search}}</el-button>
        <el-button v-if="isAuth('api:importantmessage:add')" type="primary" @click="handleAdd()">{{text_add}}</el-button>
        <el-button v-if="isAuth('api:importantmessage:delete')" type="danger" @click="handleBatchDelete()" :disabled="dataListSelections.length <= 0">{{text_batch_delete}}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="handleMultiSelectionChange"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="id">
      </el-table-column>
      <el-table-column
        prop="time"
        header-align="center"
        align="center"
        label="时间">
      </el-table-column>
      <el-table-column
        prop="autoid"
        header-align="center"
        align="center"
        label="直播室id">
      </el-table-column>
      <el-table-column
        prop="prevalue"
        header-align="center"
        align="center"
        label="前值">
      </el-table-column>
      <el-table-column
        prop="annvalue"
        header-align="center"
        align="center"
        label="实值">
      </el-table-column>
      <el-table-column
        prop="day"
        header-align="center"
        align="center"
        label="用户的手机号">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="importence"
        header-align="center"
        align="center"
        label="重要信息提示">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="question"
        header-align="center"
        align="center"
        label="重要话题">
      </el-table-column>
      <el-table-column
        prop="imageurl"
        header-align="center"
        align="center"
        label="图片">
        <template slot-scope='scope'>
          <span v-if="StringUtil.isEmpty(scope.row.imageurl)">无</span>
          <img v-preview="scope.row.imageurl"
               :src="scope.row.imageurl"
               style="width: 50px;height: 40px"
               v-else
               alt="图片"
               preview-title-enable="false"
               preview-nav-enable="false">
        </template>
      </el-table-column>

      <!--<el-table-column-->
        <!--prop="goodpers"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="用户的手机号">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="badpers"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="用户的手机号">-->
      <!--</el-table-column>-->
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="修改时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('api:importantmessage:update')" type="text" size="small" @click="handleUpdate(scope.row)">{{text_update}}</el-button>
          <el-button v-if="isAuth('api:importantmessage:delete')" type="text" size="small" @click="handleSingleDelete(scope.row)">{{text_delete}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-show="listQuery.totalCount > 0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 50, 100]"
      :current-page="listQuery.pageIndex"
      :page-size="listQuery.pageSize"
      :total="listQuery.totalCount"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible"  :rowData="rowData" ref="addOrUpdate" @refreshDataList="requestList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './importantmessage-add-or-update'
  export default {
    data () {
      return {
        rowData: {}, // 当前行数据对象
        listQuery: {
          // 分页字段
          pageIndex: 1,
          pageSize: 10,
          totalCount: 0,
          // 筛选字段
          key: ''
        },
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.requestList()
    },
    methods: {
      /** 请求列表 */
      requestList () {
        this.dataListLoading = true
        this.$httpRequest({
          url: '/api/importantmessage/list',
          method: 'get',
          params: {
            'page': this.listQuery.pageIndex,
            'limit': this.listQuery.pageSize,
            'key': this.listQuery.key
          }
        }).then((data) => {
          this.dataListLoading = false
          this.dataList = this.formatData(data.list)
          this.listQuery.totalCount = data.totalCount
        }, error => {
          this.dataListLoading = false
          this.showErrorToast(error.msg)
        })
      },
      /** 格式化数据 */
      formatData (list) {
        if (list) {
          return list.map(item => {
         // item.createTime = this.formatTime2(item.createTime)
            item.imageurl = this.baseOssUrl + item.imageurl
            return item
          })
        }
      },
      /** 搜索过滤 */
      handleFilter () {
        this.listQuery.pageIndex = 1
        this.requestList()
      },
      /** 当前页 分页page改变 */
      handleCurrentChange (val) {
        this.listQuery.pageIndex = val
        this.requestList()
      },
      /** 每页数 分页size改变 */
      handleSizeChange (val) {
        this.listQuery.pageSize = val
        this.listQuery.pageIndex = 1
        this.requestList()
      },
      /** 多选 */
      handleMultiSelectionChange (val) {
        this.dataListSelections = val
      },
      /** 新增 */
      handleAdd () {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.initDialog('dialog_add')
        })
      },
      /** 修改 */
      handleUpdate (row) {
        this.rowData = row
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.initDialog('dialog_update')
        })
      },
      /** 单选删除  */
      handleSingleDelete (row) {
        this.rowData = row
        let selectIds = [row.id]
        this.requestDelete(selectIds)
      },
      /** 批删除 */
      handleBatchDelete () {
        let selectIds = this.dataListSelections.map(item => {
          return item.id
        })
        this.requestDelete(selectIds)
      },
      /** 请求删除 */
      requestDelete (selectIds) {
        let title = `确定对[id=${selectIds.join(',')}]进行[${selectIds.length === 1 ? '删除' : '批量删除'}]操作?`
        this.showConfirmDialog(title, () => {
          this.$httpRequest({
            url: '/api/importantmessage/delete',
            method: 'post',
            data: selectIds
          }).then(() => {
            this.showSuccessToast(this.toast_text_delete_success, () => {
              this.requestList()
            })
          }, (error) => {
            this.showErrorToast(error.msg)
          })
        })
      }
    }
  }
</script>
